# Definition for a binary tree node.
from typing import Optional, List


class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right


class Solution:
    def levelOrderBottom(self, root: Optional[TreeNode]) -> List[List[int]]:
        if root is None:
            return []

        ret = []
        line = [root]
        while line:
            ret.append([node.val for node in line])
            temp = []
            for node in line:
                if node.left:
                    temp.append(node.left)
                if node.right:
                    temp.append(node.right)
            line = temp
        ret.reverse()
        return ret
